"""
管理员相关Pydantic模式
创建时间：2025-09-12
作者：开发团队
目的：定义管理员功能相关的数据验证和序列化模式
"""

from pydantic import BaseModel, Field, field_validator
from datetime import datetime, date


# 密钥管理相关模式
class KeyCreate(BaseModel):
    """密钥创建模式"""
    created_by: int = Field(..., description="创建人ID")
    expired_at: datetime | None = Field(None, description="过期时间")


class KeyResponse(BaseModel):
    """密钥响应模式"""
    id: int
    content: str
    status: str
    created_by: int
    created_at: datetime
    expired_at: datetime | None = None

    class Config:
        orm_mode = True


# 特殊访客管理相关模式
class SpecialVisitorBase(BaseModel):
    """特殊访客基础模式"""
    name: str = Field(..., min_length=1, max_length=100, description="姓名")
    phone: str = Field(..., description="手机号")
    id_card: str | None = Field(None, description="身份证号")
    type: str = Field(..., pattern="^(VIP|纠纷|其他)$", description="类型")
    valid_until: date = Field(..., description="有效期")
    remarks: str | None = Field(None, description="备注")
    campus_id: int | None = Field(None, description="院区ID")


class SpecialVisitorCreate(SpecialVisitorBase):
    """特殊访客创建模式"""
    pass


class SpecialVisitorUpdate(BaseModel):
    """特殊访客更新模式"""
    name: str | None = Field(None, min_length=1, max_length=100, description="姓名")
    phone: str | None = Field(None, description="手机号")
    id_card: str | None = Field(None, description="身份证号")
    type: str | None = Field(None, pattern="^(VIP|纠纷|其他)$", description="类型")
    valid_until: date | None = Field(None, description="有效期")
    remarks: str | None = Field(None, description="备注")
    campus_id: int | None = Field(None, description="院区ID")


class SpecialVisitorResponse(SpecialVisitorBase):
    """特殊访客响应模式"""
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        orm_mode = True


# 门卫管理相关模式
class GuardBase(BaseModel):
    """门卫基础模式"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    name: str = Field(..., min_length=1, max_length=100, description="姓名")
    phone: str | None = Field(None, description="手机号")
    campus_id: int | None = Field(None, description="院区ID")
    status: str = Field("active", pattern="^(active|disabled)$", description="状态")


class GuardCreate(GuardBase):
    """门卫创建模式"""
    password: str = Field(..., min_length=6, max_length=50, description="密码")

    @field_validator('username')
    @classmethod
    def username_alphanumeric(cls, v: str) -> str:
        assert v.isalnum() or v.replace('_', '').isalnum(), '用户名只能包含字母、数字和下划线'
        return v


class GuardUpdate(BaseModel):
    """门卫更新模式"""
    name: str | None = Field(None, min_length=1, max_length=100, description="姓名")
    phone: str | None = Field(None, description="手机号")
    campus_id: int | None = Field(None, description="院区ID")
    status: str | None = Field(None, pattern="^(active|disabled)$", description="状态")


class GuardResponse(GuardBase):
    """门卫响应模式"""
    id: int
    created_at: datetime
    last_login: datetime | None = None

    class Config:
        orm_mode = True


# 审计日志相关模式
class AuditLogParams(BaseModel):
    """审计日志查询参数"""
    start_date: str | None = Field(None, description="开始日期")
    end_date: str | None = Field(None, description="结束日期")
    user_id: int | None = Field(None, description="用户ID")
    action_type: str | None = Field(None, description="操作类型")
    skip: int | None = Field(0, description="跳过的记录数")
    limit: int | None = Field(100, description="返回的记录数")


class AuditLogResponse(BaseModel):
    """审计日志响应模式"""
    id: int
    user_id: int
    action_type: str
    target_type: str | None = None
    target_id: int | None = None
    detail: str | None = None
    ip_address: str | None = None
    created_at: datetime

    class Config:
        orm_mode = True


class ChangePasswordRequest(BaseModel):
    """修改密码请求模式"""
    new_password: str = Field(..., min_length=6, max_length=50, description="新密码")